<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form
          :inline="true"
          label-width="90px"
          :model="queryForm"
          @submit.native.prevent
        >
          <el-form-item label="设备名称" class="ml-2">
            <el-select
              multiple
              filterable
              allow-create
              v-model="queryForm.DeviceId"
              placeholder="选择设备名称"
            >
              <el-option
                v-for="item in DeviceInfoData"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="日期">
            <el-date-picker
              v-model="queryForm.date"
              type="datetimerange"
              range-separator="To"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      ref="tableSortRef"
      v-loading="listLoading"
      :border="border"
      :data="list"
      :height="height"
      :size="lineHeight"
      :stripe="stripe"
      @selection-change="setSelectRows"
    >
      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :prop="item.prop"
        :sortable="item.sortable"
        width="auto"
      >
        <template slot-scope="scope">
          <template v-if="item.label === '状态'">
            <el-text v-if="scope.row[item.prop] == '待机'" type="success">
              待机
            </el-text>
            <el-text v-else-if="scope.row[item.prop] == '关机'" type="info">
              关机
            </el-text>
            <!-- <el-text v-else-if="scope.$index == 2" type="warning">待机</el-text>
            <el-text v-else type="danger">报警</el-text> -->
          </template>
          <span>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>
      <template slot="empty">
        <el-empty class="vab-data-empty" description="暂无数据" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { UsageStatistics, EquipmentLists } from '@/api/equipment'

  export default {
    data() {
      return {
        TrayInfoData: [],
        form: {
          Id: '',
          MaterialType: '',
          Content: '',
          ApplyProduct: '',
          Creator: '',
          CreateTime: new Date(),
          MaterialNum: '',
        },
        radio: 3,
        formLabelWidth: '100px',
        dialogVisible: false,
        dialogVisible1: false,
        tableSortRef: null,
        editRef: null,
        border: true,
        height: this.$baseTableHeight(1),
        stripe: false,
        lineHeight: 'small',
        isFullscreen: false,
        checkList: ['状态', '开始时间', '结束时间', '持续时间'],
        columns: [
          {
            label: '状态',
            prop: 'State',

            disableCheck: true,
          },
          {
            label: '开始时间',
            prop: 'StartTime',
          },
          {
            label: '结束时间',
            prop: 'EndTime',
          },
          {
            label: '持续时间',
            prop: 'KeepTime',
          },
        ],
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          DeviceId: '',
          date: '',
          MatchString: '',
        },
        title: '添加',
        DeviceInfoData: [],
      }
    },
    computed: {
      finallyColumns() {
        return this.columns.filter((item) =>
          this.checkList.includes(item.label)
        )
      },
    },
    created() {
      this.fetchData()
      EquipmentLists().then((res) => {
        console.log('10001000', res.DeviceIds)
        this.DeviceInfoData = res.DeviceIds
      })
    },
    methods: {
      handleLine(row) {
        this.dialogVisible = true
        this.productLineData = row.ProductLine
      },
      handleSelectionChange(e) {
        this.form.ApplyProduct = e.map((item) => {
          return item.ProducationName
        })
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      submit() {
        this.form.CreateTime = this.days()
        MaterialType(this.form).then((res) => {
          console.log(res)
          this.$baseMessage('成功', 'success', 'vab-hey-message-success')
          this.dialogVisible = false
          fetchData()
        })
      },
      handleEdit(row) {
        this.productData()
        if (row.DeviceId) {
          this.title = '编辑'
          this.form = row
        } else {
          this.title = '添加'
        }

        this.dialogVisible = true
      },
      productData() {
        GetProduction().then((res) => {
          console.log('xxxxxxx', res.ProductInfo)
          this.ProductInfoData = res.ProductInfo
        })
      },
      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await EquipmentDelete({
            DeviceId: row.DeviceId,
          })
          // $baseMessage(msg, 'success', 'vab-hey-message-success')
          await fetchData()
        })
        return false
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleClose() {
        this.dialogVisible = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const params = {
          Index: this.getPage(this.queryForm.pageNo, this.queryForm.pageSize),
          Count: this.queryForm.pageSize,
          DeviceId: this.queryForm.DeviceId,
          StartTime: this.days(this.queryForm.date[0]),
          EndTime: this.days(this.queryForm.date[1]),
        }
        UsageStatistics(params).then((res) => {
          this.list = res.DeviceUsageInfo
          this.total = res.TotalCount
          this.listLoading = false
        })
      },
    },
  }
</script>
